﻿@model UrlRecordListModel
@inject AdminAreaSettings adminAreaSettings
@{
    //page title
    ViewBag.Title = T("Admin.System.SeNames").Text;
}


<div class="row">
    <div class="col-md-12">
        <div class="x_panel light form-fit">
            <div class="x_title">
                <div class="caption">
                    <i class="fa fa-sitemap"></i>
                    @T("Admin.System.SeNames")
                </div>
                <vc:admin-widget widget-zone="senames_buttons" additional-data="null" />
                <div class="actions btn-group btn-group-devided">
                    <span id="delete-selected" class="btn red"><i class="fa fa-trash-o"></i> @T("Admin.System.SeNames.DeleteSelected")</span>
                </div>
            </div>
            <div class="x_content form">
                <vc:admin-widget widget-zone="senames_top" additional-data="null" />
                <div class="form-horizontal">
                    <div class="form-body">
                        <div class="form-group">
                            <admin-label asp-for="SeName" />
                            <div class="col-md-4 col-sm-6">
                                <admin-input asp-for="SeName" />
                            </div>
                        </div>
                        <div class="form-group">
                            <admin-label asp-for="SearchActiveId"/>
                            <div class="col-4 col-md-6">
                                <admin-select asp-for="SearchActiveId" asp-items="Model.AvailableActiveOptions" />
                            </div>
                        </div>
                    </div>
                    <div class="form-actions">
                        <div class="row">
                            <div class="offset-md-3 offset-sm-3 col-md-9 col-sm-9">
                                <button class="btn btn-success filter-submit" id="search-senames">
                                    <i class="fa fa-search"></i> @T("Admin.Common.Search")
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <vc:admin-widget widget-zone="senames_bottom" additional-data="null" />
            </div>
            <div class="x_content">
                <div id="sename-grid"></div>
            </div>
        </div>
    </div>
</div>

<script>
    $(document).ready(function () {
        $("#sename-grid").kendoGrid({
            dataSource: {
                transport: {
                    read: {
                        url: "@Html.Raw(Url.Action("SeNames", "Common"))",
                        type: "POST",
                        dataType: "json",
                        data: additionalData
                    }
                },
                schema: {
                    data: "Data",
                    total: "Total",
                    errors: "Errors"
                },
                error: function(e) {
                    display_kendoui_grid_error(e);
                    // Cancel the changes
                    this.cancelChanges();
                },
                pageSize: @(adminAreaSettings.DefaultGridPageSize),
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true
            },
            pageable: {
                refresh: true,
                pageSizes: [@(adminAreaSettings.GridPageSizes)]
            },
            scrollable: false,
            dataBound: onDataBound,
            columns: [{
                field: "Id",
                headerTemplate: "<label class='mt-checkbox mt-checkbox-outline control control-checkbox'><input id='mastercheckbox' type='checkbox'/> <div class='control__indicator'></div></label>",
                headerAttributes: { style: "text-align:center" },
                attributes: { style: "text-align:center" },
                template: "<label class='mt-checkbox mt-checkbox-outline control control-checkbox'><input type='checkbox' value='#=Id#' class='checkboxGroups'/> <div class='control__indicator'></div></label>",
                width: 50
            }, {
                field: "Name",
                title: "@T("Admin.System.SeNames.Name")",
                width: 300,
                template: '# if(DetailsUrl && DetailsUrl.length > 0) {# <a class="k-link" href="#=DetailsUrl#">#=kendo.htmlEncode(Name)#</a> #} #'
            }, {
                field: "EntityName",
                title: "@T("Admin.System.SeNames.EntityName")",
                width: 100
            }, {
                field: "IsActive",
                title: "@T("Admin.System.SeNames.IsActive")",
                width: 50,
                headerAttributes: { style: "text-align:center" },
                attributes: { style: "text-align:center" },
                template: '# if(IsActive) {# <i class="fa fa-check" aria-hidden="true" style="color:green"></i> #} else {# <i class="fa fa-times" aria-hidden="true" style="color:red"></i> #} #'
            }, {
                field: "Language",
                title: "@T("Admin.System.SeNames.Language")",
                width: 100
            }]
        });
    });
</script>

<script>
    var selectedIds = [];

    $(document).ready(function() {

        //search button
        $('#search-senames').click(function() {
            //search
            var grid = $('#sename-grid').data('kendoGrid');
            grid.dataSource.page(1); //new search. Set page size to 1
            //grid.dataSource.read(); we already loaded the grid above using "page" function
            //clear selected checkboxes
            $('.checkboxGroups').prop('checked', false).change();
            selectedIds = [];
            return false;
        });

        $("#@Html.FieldIdFor(model => model.SeName)").keydown(function(event) {
            if (event.keyCode == 13) {
                $("#search-senames").click();
                return false;
            }
        });

        //"delete selected" button
        $('#delete-selected').click(function(e) {
            e.preventDefault();

            var postData = {
                selectedIds: selectedIds
            };
            addAntiForgeryToken(postData);

            $.ajax({
                cache: false,
                type: "POST",
                url: "@(Url.Action("DeleteSelectedSeNames", "Common"))",
                data: postData,
                complete: function(data) {
                    //reload grid
                    var grid = $('#sename-grid').data('kendoGrid');
                    grid.dataSource.page(1); //new search. Set page size to 1
                    //grid.dataSource.read(); we already loaded the grid above using "page" function
                    //clear selected checkboxes
                    $('.checkboxGroups').prop('checked', false).change();
                    selectedIds = [];
                },
                error: function(xhr, ajaxOptions, thrownError) {
                    alert(thrownError);
                },
                traditional: true
            });
            return false;
        });

        $('#mastercheckbox').click(function() {
            $('.checkboxGroups').prop('checked', $(this).is(':checked')).change();
        });

        //wire up checkboxes.
        $('#sename-grid').on('change', 'input[type=checkbox][id!=mastercheckbox]', function(e) {
            var $check = $(this);
            if ($check.is(":checked") == true) {
                var checked = jQuery.inArray($check.val(), selectedIds);
                if (checked == -1) {
                    //add id to selectedIds.
                    selectedIds.push($check.val());
                }
            } else {
                var checked = jQuery.inArray($check.val(), selectedIds);
                if (checked > -1) {
                    //remove id from selectedIds.
                    selectedIds = $.grep(selectedIds, function(item, index) {
                        return item != $check.val();
                    });
                }
            }
            updateMasterCheckbox();
        });
    });

    function additionalData() {
        var data = {
            SeName: $('#@Html.FieldIdFor(model => model.SeName)').val(),
            SearchActiveId: $('#@Html.FieldIdFor(model => model.SearchActiveId)').val()
        };
        addAntiForgeryToken(data);
        return data;
    }

    function onDataBound(e) {

        $('#sename-grid input[type=checkbox][id!=mastercheckbox]').each(function() {
            var currentId = $(this).val();
            var checked = jQuery.inArray(currentId, selectedIds);
            //set checked based on if current checkbox's value is in selectedIds.
            $(this).prop('checked', checked > -1);
        });

        updateMasterCheckbox();
    }

    function updateMasterCheckbox() {
        var numChkBoxes = $('#sename-grid input[type=checkbox][id!=mastercheckbox]').length;
        var numChkBoxesChecked = $('#sename-grid input[type=checkbox][id!=mastercheckbox]:checked').length;
        $('#mastercheckbox').prop('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
    }
</script>